package com.hnluchuan.bmjlearning.service;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnluchuan.bmjlearning.dao.CustomerDAO;
import com.hnluchuan.bmjlearning.dao.CustomerServicePackageDAO;
import com.hnluchuan.bmjlearning.model.Customer;
import com.hnluchuan.utils.common.DateUtils;

@Service
public class CoreService {
	@Autowired
	private CacheService cacheService;
	@Autowired
	private CustomerDAO customerDAO;

	@Autowired
	private CustomerServicePackageDAO customerServicePackageDAO;
	
	/**
	 * 检查服务包是否有效。
	 * 如果无效的话就不能访问在线学习页面
	 * @return
	 */
	public boolean isServicePackageValid(Long userId) {
		Customer customer = customerDAO.findByUserId(userId);
		if (customer.getExpireDate() == null 
				|| DateUtils.getEndOfADay(customer.getExpireDate()).before(new Date())) {
			return false;
		}
		return true;
	}
	
	public String generateTokenAndSetToCache(Long userId) {
		String uuid = UUID.randomUUID().toString();
		cacheService.setString(uuid, userId.toString(), (int)TimeUnit.DAYS.toSeconds(1)); // 一天的有效期
		return uuid;
	}
}
